import pandas as pd
import numpy as np
from scipy import stats

def process(df: pd.DataFrame, threshold: float = 3.0) -> pd.DataFrame:
    '''使用插值方法处理离群值
    
    Args:
        df: 输入数据框
        threshold: Z-score阈值，默认为3
    '''
    try:
        # 计算用电量的Z值
        z_scores = np.abs(stats.zscore(df.iloc[:, 1]))      
        # 标记离群值为NaN
        outlier_count = np.sum(z_scores > threshold)
        df.iloc[:, 1] = np.where(z_scores > threshold, np.nan, df.iloc[:, 1])       
        # 使用插值填充
        df.iloc[:, 1] = df.iloc[:, 1].interpolate(method='linear')   
        print(f"interpolate: 替换了{outlier_count}个离群值")
        return df  
    except Exception as e:
        print(f"interpolate替换处理错误: {str(e)}")
        return None 